package bigoo.peer;

import java.util.ArrayList;

import eduni.simjava.Sim_entity;
import eduni.simjava.Sim_port;
import eduni.simjava.Sim_system;
import eduni.simjava.distributions.Sim_negexp_obj;

public class PublisherSource extends Sim_entity {

    private final Sim_port out;
    private final Sim_negexp_obj delayGenerator;
    private final String peerID;
    public static ArrayList<RideRequest> rides = new ArrayList<RideRequest>();

    public PublisherSource(String peerID, double mean) {
	super(peerID + "Publisher");
	this.out = new Sim_port("out");
	this.add_port(this.out);
	this.peerID = peerID;
	this.delayGenerator = new Sim_negexp_obj("PublisherDelay", mean);
    }

    @Override
    public void body() {
	while (Sim_system.running()) {
	    this.sim_pause(this.delayGenerator.sample());
	    RideRequest request = this.getRide();
	    rides.add(request);
	    this.sim_schedule(this.out, 0.0, BigooPeer.LOCAL_PUBLISH_EVENT, request);
	}
	rides = new ArrayList<RideRequest>();
    }

    private RideRequest getRide() {
	int dest = (int) (Math.random() * 1000);
	int date = (int) (Math.random() * 1000);

	RideRequest req = new RideRequest(dest, date, 5, this.peerID);
	return req;
    }

    public static class RideRequest {
	private final int date;
	private final int dest;
	private final int places;
	private final String peerID;

	public RideRequest(int dest, int date, int places, String peerID) {
	    this.date = date;
	    this.dest = dest;
	    this.places = places;
	    this.peerID = peerID;
	}

	public int getDate() {
	    return this.date;
	}

	public int getDest() {
	    return this.dest;
	}

	public int getPlaces() {
	    return this.places;
	}

	/**
	 * @return the peerID
	 */
	public String getPeerID() {
	    return this.peerID;
	}

	@Override
	public String toString() {
	    return this.peerID + "|" + this.dest + "|" + this.date;
	}
    }
}
